package com.lantosec.hmrp.modules.form.controller;

import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.form.entity.FormColumns;
import com.lantosec.hmrp.modules.form.entity.FormTable;
import com.lantosec.hmrp.modules.form.service.IFormColumnsService;
import com.lantosec.hmrp.modules.form.vo.CalculateValueVO;
import com.lantosec.hmrp.modules.form.vo.FormColumnsVO;
import com.lantosec.hmrp.modules.form.vo.UpdateCommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * 自定义表单列定义 控制器
 *
 * @author AutoCode
 * @since 2020-04-15
 */
@Api(value = "自定义表单列定义控制器", tags = "自定义表单列定义控制器")
@RestController
@RequestMapping("/form/columns/")
@Log4j2
public class FormColumnsController extends BaseController {
    @Resource
    private IFormColumnsService service;

    @GetMapping("queryByTableInfo")
    @ApiOperation(value = "根据表的信息查询列集合")
    public Result<List<FormColumnsVO>> queryByTableInfo(@ApiParam(value = "表单信息", required = true) FormTable condition) {
        List<FormColumnsVO> list = service.queryByTableInfo(condition.getTableSchema(), condition.getTableName(), condition.getTableType());
        return Result.ok(list);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<FormColumnsVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("create")
    @ControllerLog(name = "自定义表单列新增", type = LogType.add)
    @ApiOperation(value = "新增数据")
    public Result<String> create(@ApiParam(value = "自定义表单列定义VO对象", required = true) @RequestBody @Validated FormColumnsVO vo){
        FormColumns entity = vo.convertToEntity();
        entity.setCreateUserId(super.getUserId());
        entity.setCreateUserName(super.getName());
        return service.saveColumn(entity);
    }

    @PutMapping("update")
    @ControllerLog(name = "自定义表单列修改数据", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "自定义表单列定义VO对象", required = true) @RequestBody FormColumnsVO vo){
        FormColumns entity = vo.convertToEntity();
        entity.setCreateUserId(super.getUserId());
        entity.setCreateUserName(super.getName());
        return service.updateColumn(entity);
    }

    @PutMapping("updateComment")
    @ControllerLog(name = "自定义表单列修改字段描述", type = LogType.update)
    @ApiOperation(value = "修改字段描述")
    public Result<Void> updateComment(@ApiParam(value = "修改字段描述VO对象", required = true) @RequestBody UpdateCommentVO vo) {
        return service.updateColumnComment(vo);
    }

    @DeleteMapping(value = "delete/{id}")
    @ControllerLog(name = "自定义表单列删除字段", type = LogType.delete)
    @ApiOperation("删除")
    public Result<Void> delete(@ApiParam(value = "Id", required = true) @PathVariable(name="id") String id) {
        if (StringUtils.isNotEmpty(id)) {
            Boolean flag = service.removeById(id);
            if (!flag) {
                return Result.fail("删除失败");
            }
        }
        return Result.ok();
    }

    @GetMapping("query/{tableId}")
    @ApiOperation(value = "根据表格Id查询")
    public Result<List<FormColumnsVO>> query(@ApiParam(value = "表格Id", required = true) @PathVariable(name="tableId") String tableId) {
        return Result.ok(entity2VO(service.queryByTableId(tableId, true)));
    }

    @GetMapping("queryNotToCamelCase/{tableId}")
    @ApiOperation(value = "根据表格Id查询，字段名不转驼峰格式")
    public Result<List<FormColumnsVO>> queryNotToCamelCase(@ApiParam(value = "表格Id", required = true) @PathVariable(name="tableId") String tableId) {
        return Result.ok(entity2VO(service.queryByTableId(tableId, false)));
    }

    @GetMapping("synchronize/{tableId}")
    @ControllerLog(name = "自定义表单列同步新添加的列", type = LogType.add)
    @ApiOperation(value = "同步新添加的列")
    public Result<Boolean> synchronizeColumn(@ApiParam(value = "表格Id", required = true) @PathVariable(name="tableId") String tableId) {
        return service.synchronizeColumn(tableId);
    }

    @PostMapping("bindValue")
    @ControllerLog(name = "根据字段Id和表单数据查询字段绑定的值", type = LogType.other)
    @ApiOperation(value = "根据字段Id和表单数据查询字段绑定的值")
    public Result<Object> bindValue(@ApiParam(value = "表单数据", required = true) @RequestBody String formData) {
        return service.bindColumnValue(formData);
    }

    @PostMapping("getFieldBySql")
    @ApiOperation(value = "根据查询sql获取查询的字段列")
    public Result<List<String>> getFieldBySql(@ApiParam(value = "sql", required = true) @RequestBody String sql){
        return Result.ok(service.getFieldBySql(sql));
    }
}
